<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use App\Http\Requests\CreatePageRequest;
use App\Page;
use MyFlash;
class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list=Page::where('type',2)->get();
        return view('admin.page.index',compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $about=Page::where('type','=',1)->first();
        return view('admin.page.create',compact('about'));
    }
     //加载添加招贤纳士页面
     public function createzx()
     {
         // 加载公司简介视图
         return view('admin.page.createzx');
     }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=[
            'type'=>$request->type,
            'title'=>$request->title,
            'remark'=>isset($request->remark)?$request->remark:'',
            'pic'=>isset($request->pic)?$request->pic:'',
            'content'=>isset($request->contents)?$request->contents:'',
        ];
        if($request->file('pic')){
            $data->pic=$this->picUploads($request);
        }
        switch($request->type){
            case "1":
             $result = Page::updateOrCreate(['type'=>1],$data);
                MyFlash::success('公司简介添加成功');
               return back();
            break;
            case "2":
            Page::updateOrCreate(['title'=>$request->title],$data);
            MyFlash::success('职位添加成功');
               return redirect(route('admin.page.index'));
            break;
            case "3":
            Page::updateOrCreate(['title'=>$request->title],$data);
            MyFlash::success('历程添加成功');
               return redirect(route('admin.page.lists'));
            break;
        }
    }
    //图片上传
    public function picUploads($request){
        if($request->file('pic')){
            $thumb=$request->pic;
            $name=$thumb->hashName();
            $thumb->storeAs('/public/uploads/',$name);
            return $name;
        }
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $page=Page::findOrFail($id);
        return view('admin.page.edit',compact('page'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $page=Page::findOrFail($id);
        $page->title=$request->title;
        $page->content=$request->contents;
        if($request->file('pic')){
            $this->exitsimg($page->pic);
            $page->pic=$this->picUploads($request);
        }
        $page->save();
        if($page->type==2){
            MyFlash::success('职位编辑成功');
        return redirect(route('admin.page.index'));
        }else{
            MyFlash::success('历程编辑成功');
        return redirect(route('admin.page.lists'));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Page $page)
    {
        $page->delete();
        MyFlash::success('删除成功');
        if($page->type==2){
            return redirect(route('admin.page.index'));
        }else{
            return redirect(route('admin.page.lists'));
        }

    }
    /**
     * 发展历程列表
     */
    public function lists(){
        $list=Page::where('type',3)->get();
        return view('admin.page.lists',compact('list'));
    }
    /**
     * 发展添加模板
     */
    public function add(){
        return view('admin.page.add');
    }
    /**
     * 发展历程编辑模板
     */
    public function editlc(Page $page){
        return view('admin.page.editlc',compact('page'));
    }

    // 检查文件是否存在
    public function exitsimg($img){
        $path='public/uploads/'.$img;
        $exists = Storage::disk('local')->exists($path);
        if ($exists) {
            $this->delimg($path);
        }
    }
    // 删除图片
    public function delimg($path){
        $result=Storage::delete($path);
        if ($result) {
            return true;
        }else {
            return false;
        }
    }
}
